//
//  TSUserModel.m
//  chart
//
//  Created by xu on 15/7/26.
//  Copyright (c) 2015年 xu. All rights reserved.
//

#import "TSUserModel.h"
#import "XGPush.h"
#import "XGPushService.h"

@implementation TSUserModel

- (NSDictionary *)modelKeyJSONKeyMapper
{
    return @{@"userId":@"id"};
}

+ (instancetype)sharedInstance {
    
    static TSUserModel *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[TSUserModel alloc] init];
    });
    
    return sharedInstance;
}

- (void)loginWithUser:(NSString *)userName
          andPassword:(NSString *)pwd
    completionHandler:(void(^)(BOOL isSucceeded, NSError *error))handler
{
    self.requestParams = @{@"userName":userName?:@"", @"passWord":pwd?:@"", @"lastType":@"4"};
    
    WS(weakSelf);
    [self fetchWithPath:kRequestUrl_UserLogin completionHandler:^(BOOL isSucceeded, NSError *error) {
        
        handler(isSucceeded, error);
        
        if (isSucceeded) {
            [USERDEFAULTS setValue:userName forKey:TSUserDefaultsKey_UserName];
            if([USERDEFAULTS boolForKey:TSUserDefaultsKey_RememberPwd]){
                [USERDEFAULTS setValue:pwd forKey:TSUserDefaultsKey_Password];
            }else{
                [USERDEFAULTS removeObjectForKey:TSUserDefaultsKey_Password];
            }
            [[LBToastManager manager] showtoast:@"登录成功"];
            [XGPush setAccount:weakSelf.userName];
            [XGPushService didRegisterForRemoteNotificationsWithDeviceToken:[TSTools getDeviceToken]];
        }
    } NoProgress:NO];
}

- (void)logout:(void(^)(BOOL isSucceeded, NSError *error))handler
{
    [self fetchWithPath:kRequestUrl_UserLogout completionHandler:^(BOOL isSucceeded, NSError *error) {
        
        handler(isSucceeded, error);
        
        if (isSucceeded) {
            [[LBToastManager manager] showtoast:@"退出登录成功"];
        }
        else{
            [[LBToastManager manager] showtoast:@"退出登录失败"];
        }
    } NoProgress:NO];
}

- (void)modify:(NSString *)name
     andWorkId:(NSString *)workId andPhone:(NSString *)phone
completionHandler:(void(^)(BOOL isSucceeded, NSError *error))handler
{
    WS(weakSelf);
    self.requestParams = @{TSDictionaryKeyDefine_Name:name?:@"", TSDictionaryKeyDefine_Phone:phone?:@"", TSDictionaryKeyDefine_WorkId:workId?:@""};
    [self fetchWithPath:kRequestUrl_User_setDetail completionHandler:^(BOOL isSucceeded, NSError *error) {
        
        handler(isSucceeded, error);
        
        __strong typeof (weakSelf)strongSelf = weakSelf;
        if (isSucceeded) {
            [[LBToastManager manager] showtoast:@"修改成功"];
            
            strongSelf->_name = name;
            strongSelf->_workId = workId;
            strongSelf->_phone = phone;
        }
        else{
            [[LBToastManager manager] showtoast:@"修改失败"];
        }
    } NoProgress:NO];
}

- (void)modifyPwd:(NSString *)oldPwd andNewPwd:(NSString *)newPwd completionHandler:(void(^)(BOOL isSucceeded, NSError *error))handler
{
    WS(weakSelf);
    self.requestParams = @{TSDictionaryKeyDefine_PassWordOld:oldPwd?:@"", TSDictionaryKeyDefine_PassWord:newPwd?:@""};
    [self fetchWithPath:kRequestUrl_User_updatePassword completionHandler:^(BOOL isSucceeded, NSError *error) {
        
        handler(isSucceeded, error);
        
        __strong typeof (weakSelf)strongSelf = weakSelf;
        if (isSucceeded) {
            [[LBToastManager manager] showtoast:@"修改密码成功"];
            strongSelf->_passWord = newPwd;
            [USERDEFAULTS setValue:newPwd forKey:TSUserDefaultsKey_Password];
        }
        else{
            [[LBToastManager manager] showtoast:@"修改密码失败"];
        }
    } NoProgress:NO];
}

- (void)registerWithUser:(NSString *)userName
             andPassword:(NSString *)pwd andPhone:(NSString *)phone
       completionHandler:(void(^)(BOOL isSucceeded, NSError *error))handler
{
    self.requestParams = @{@"userName":userName?:@"", @"passWord":pwd?:@"", @"phone":phone?:@""};
    
    WS(weakSelf);
    [self fetchWithPath:kRequestUrl_UserInsert completionHandler:^(BOOL isSucceeded, NSError *error) {
        
        handler(isSucceeded, error);
        
        if (isSucceeded) {
            [[LBToastManager manager] showtoast:@"注册成功"];
            
            [XGPush setAccount:weakSelf.userName];
            
            [XGPushService didRegisterForRemoteNotificationsWithDeviceToken:[TSTools getDeviceToken]];
        }
    } NoProgress:NO];
}

@end
